
package ui.activity.poscontrol.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import base.BaseViewHolder;
import base.SuperBaseAdapter;
import ui.activity.home.PickupMachineAct;
import ui.activity.poscontrol.beanmodel.RedBallBean;
import util.SysUtils;


/**
 *POS查询列表适配器
 * Created by chenjian on 19/2/27
 */
public class RedBallAdapter extends SuperBaseAdapter<RedBallBean.RecordsBean> {

    Context mContext;
    List<RedBallBean.RecordsBean> mData=new ArrayList<>();
    public static final int SECONDS_IN_DAY = 60 * 60 * 24;
    public static final long MILLIS_IN_DAY = 1000L * SECONDS_IN_DAY;

    int msign;//1可用2已使用3已过期
    public RedBallAdapter(Context context , List<RedBallBean.RecordsBean> data, int sign) {
        super(context,data);
        mContext = context;
        msign = sign;
        mData = data;
    }
    public void refresh() {
    }

    @Override
    protected void convert(BaseViewHolder holder, final RedBallBean.RecordsBean item, final int position) {
        holder.setText(R.id.redballtype,item.getName());
        holder.setText(R.id.redballtime,"限"+ SysUtils.sf5.format(new Date(item.getCreateTime())) +"-"+SysUtils.sf5.format(new Date(item.getExpireTime())));
        holder.setText(R.id.desc,item.getComment());
        holder.setText(R.id.redballmoney,item.getAmount());
        if(msign==1) {
            if(position == 0&& isSameDayOfMillis(new Date().getTime(),item.getCreateTime())) {
                holder.getView(R.id.imagenew).setVisibility(View.VISIBLE);

            }else {
                holder.getView(R.id.imagenew).setVisibility(View.INVISIBLE);
            }
            holder.getView(R.id.gotouse).setVisibility(View.VISIBLE);
            holder.getView(R.id.redballmoney).setAlpha(1);
            holder.getView(R.id.fuhao).setAlpha(1);
            holder.getView(R.id.redballtype).setAlpha(1);
            holder.getView(R.id.redballtime).setAlpha(1);
            holder.getView(R.id.redballtime2).setAlpha(1);
            holder.setText(R.id.gotouse,"去使用");
            holder.getView(R.id.gotouse).setBackground(mContext.getResources().getDrawable(R.drawable.bg_radius_redball_red));
            holder.getView(R.id.outtimeimg).setVisibility(View.GONE);
            holder.getView(R.id.gotouse).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, PickupMachineAct.class);
                    mContext.startActivity(intent);
                }
            });
        }else  if(msign==2) {
            holder.getView(R.id.imagenew).setVisibility(View.INVISIBLE);
            holder.getView(R.id.gotouse).setVisibility(View.VISIBLE);
            holder.getView(R.id.gotouse).setBackground(mContext.getResources().getDrawable(R.drawable.bg_radius_redball_grey));
            holder.setText(R.id.gotouse,"已使用");
            holder.getView(R.id.redballmoney).setAlpha(0.7f);
            holder.getView(R.id.fuhao).setAlpha(0.7f);
            holder.getView(R.id.redballtype).setAlpha(0.7f);
            holder.getView(R.id.redballtime).setAlpha(0.7f);
            holder.getView(R.id.redballtime2).setAlpha(0.7f);
            holder.getView(R.id.outtimeimg).setVisibility(View.GONE);
        }else{
            holder.getView(R.id.imagenew).setVisibility(View.GONE);
            holder.getView(R.id.gotouse).setVisibility(View.GONE);
            holder.getView(R.id.redballmoney).setAlpha(0.7f);
            holder.getView(R.id.fuhao ).setAlpha(0.7f);
            holder.getView(R.id.redballtype).setAlpha(0.7f);
            holder.getView(R.id.redballtime2).setAlpha(0.7f);
            holder.getView(R.id.redballtime).setAlpha(0.7f);
            holder.getView(R.id.outtimeimg).setVisibility(View.VISIBLE);
        }
    }



    @Override
    protected int getItemViewLayoutId(int position, RedBallBean.RecordsBean item) {
        return R.layout.redballitem;
    }
    public static boolean isSameDayOfMillis(final long ms1, final long ms2) {
        final long interval = ms1 - ms2;
        return interval < MILLIS_IN_DAY
                && interval > -1L * MILLIS_IN_DAY
                && toDay(ms1) == toDay(ms2);
    }
    private static long toDay(long millis) {
        return (millis + TimeZone.getDefault().getOffset(millis)) / MILLIS_IN_DAY;
    }


}
